home *** CD-ROM | disk | FTP | other *** search
/ isnet Internet / Isnet Internet CD.iso / prog / hiz / 09 / 09.exe / adynware.exe / perl / lib / site / MD5.pm < prev    next >
Encoding:
Perl POD Document  |  1999-12-28  |  6.2 KB  |  251 lines

  1. package MD5;
  2.  
  3. use strict;
  4. use vars qw($VERSION @ISA @EXPORT);
  5.  
  6. require Exporter;
  7. require DynaLoader;
  8. require AutoLoader;
  9.  
  10. @ISA = qw(Exporter AutoLoader DynaLoader);
  11. @EXPORT = qw(
  12.     
  13. );
  14. $VERSION = '1.7';
  15.  
  16. bootstrap MD5 $VERSION;
  17.  
  18.  
  19. sub addfile
  20. {
  21.     no strict 'refs';    # Countermand any strct refs in force so that we
  22.  
  23.     my ($self, $handle) = @_;
  24.     my ($package, $file, $line) = caller;
  25.     my ($data) = '';
  26.  
  27.     if (!ref($handle))
  28.     {
  29.  
  30.     $handle = $package . '::' . $handle unless ($handle =~ /(\:\:|\')/);
  31.     }
  32.  
  33.     while (read($handle, $data, 1024))
  34.     {
  35.     $self->add($data);
  36.     }
  37. }
  38.  
  39. sub hexdigest
  40. {
  41.     my ($self) = shift;
  42.  
  43.     unpack("H*", ($self->digest()));
  44. }
  45.  
  46. sub hash
  47. {
  48.     my ($self, $data) = @_;
  49.  
  50.     if (ref($self))
  51.     {
  52.  
  53.     $self->reset();
  54.     }
  55.     else
  56.     {
  57.  
  58.     $self = new MD5;
  59.     }
  60.  
  61.  
  62.     $self->add($data);
  63.     $self->digest();
  64. }
  65.  
  66. sub hexhash
  67. {
  68.     my ($self, $data) = @_;
  69.  
  70.     unpack("H*", ($self->hash($data)));
  71. }
  72.  
  73.  
  74. 1;
  75. __END__
  76.  
  77. =head1 NAME
  78.  
  79. MD5 - Perl interface to the RSA Data Security Inc. MD5 Message-Digest Algorithm
  80.  
  81. =head1 SYNOPSIS
  82.  
  83.     use MD5;
  84.     
  85.     $context = new MD5;
  86.     $context->reset();
  87.     
  88.     $context->add(LIST);
  89.     $context->addfile(HANDLE);
  90.     
  91.     $digest = $context->digest();
  92.     $string = $context->hexdigest();
  93.  
  94.     $digest = MD5->hash(SCALAR);
  95.     $string = MD5->hexhash(SCALAR);
  96.  
  97. =head1 DESCRIPTION
  98.  
  99. The B<MD5> module allows you to use the RSA Data Security Inc. MD5
  100. Message Digest algorithm from within Perl programs.
  101.  
  102. A new MD5 context object is created with the B<new> operation.
  103. Multiple simultaneous digest contexts can be maintained, if desired.
  104. The context is updated with the B<add> operation which adds the
  105. strings contained in the I<LIST> parameter. Note, however, that
  106. C<add('foo', 'bar')>, C<add('foo')> followed by C<add('bar')> and
  107. C<add('foobar')> should all give the same result.
  108.  
  109. The final message digest value is returned by the B<digest> operation
  110. as a 16-byte binary string. This operation delivers the result of
  111. B<add> operations since the last B<new> or B<reset> operation. Note
  112. that the B<digest> operation is effectively a destructive, read-once
  113. operation. Once it has been performed, the context must be B<reset>
  114. before being used to calculate another digest value.
  115.  
  116. Several convenience functions are also provided. The B<addfile>
  117. operation takes an open file-handle and reads it until end-of file in
  118. 1024 byte blocks adding the contents to the context. The file-handle
  119. can either be specified by name or passed as a type-glob reference, as
  120. shown in the examples below. The B<hexdigest> operation calls
  121. B<digest> and returns the result as a printable string of hexdecimal
  122. digits. This is exactly the same operation as performed by the
  123. B<unpack> operation in the examples below.
  124.  
  125. The B<hash> operation can act as either a static member function (ie
  126. you invoke it on the MD5 class as in the synopsis above) or as a
  127. normal virtual function. In both cases it performs the complete MD5
  128. cycle (reset, add, digest) on the supplied scalar value. This is
  129. convenient for handling small quantities of data. When invoked on the
  130. class a temporary context is created. When invoked through an already
  131. created context object, this context is used. The latter form is
  132. slightly more efficient. The B<hexhash> operation is analogous to
  133. B<hexdigest>.
  134.  
  135. =head1 EXAMPLES
  136.  
  137.     use MD5;
  138.     
  139.     $md5 = new MD5;
  140.     $md5->add('foo', 'bar');
  141.     $md5->add('baz');
  142.     $digest = $md5->digest();
  143.     
  144.     print("Digest is " . unpack("H*", $digest) . "\n");
  145.  
  146. The above example would print out the message
  147.  
  148.     Digest is 6df23dc03f9b54cc38a0fc1483df6e21
  149.  
  150. provided that the implementation is working correctly.
  151.  
  152. Remembering the Perl motto ("There's more than one way to do it"), the
  153. following should all give the same result:
  154.  
  155.     use MD5;
  156.     $md5 = new MD5;
  157.  
  158.     die "Can't open /etc/passwd ($!)\n" unless open(P, "/etc/passwd");
  159.  
  160.     seek(P, 0, 0);
  161.     $md5->reset;
  162.     $md5->addfile(P);
  163.     $d = $md5->hexdigest;
  164.     print "addfile (handle name) = $d\n";
  165.  
  166.     seek(P, 0, 0);
  167.     $md5->reset;
  168.     $md5->addfile(\*P);
  169.     $d = $md5->hexdigest;
  170.     print "addfile (type-glob reference) = $d\n";
  171.  
  172.     seek(P, 0, 0);
  173.     $md5->reset;
  174.     while (<P>)
  175.     {
  176.         $md5->add($_);
  177.     }
  178.     $d = $md5->hexdigest;
  179.     print "Line at a time = $d\n";
  180.  
  181.     seek(P, 0, 0);
  182.     $md5->reset;
  183.     $md5->add(<P>);
  184.     $d = $md5->hexdigest;
  185.     print "All lines at once = $d\n";
  186.  
  187.     seek(P, 0, 0);
  188.     $md5->reset;
  189.     while (read(P, $data, (rand % 128) + 1))
  190.     {
  191.         $md5->add($data);
  192.     }
  193.     $d = $md5->hexdigest;
  194.     print "Random chunks = $d\n";
  195.  
  196.     seek(P, 0, 0);
  197.     $md5->reset;
  198.     undef $/;
  199.     $data = <P>;
  200.     $d = $md5->hexhash($data);
  201.     print "Single string = $d\n";
  202.  
  203.     close(P);
  204.  
  205. =head1 NOTE
  206.  
  207. The MD5 extension may be redistributed under the same terms as Perl.
  208. The MD5 algorithm is defined in RFC1321. The basic C code implementing
  209. the algorithm is derived from that in the RFC and is covered by the
  210. following copyright:
  211.  
  212. =over 8
  213.  
  214. Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All
  215. rights reserved.
  216.  
  217. License to copy and use this software is granted provided that it
  218. is identified as the "RSA Data Security, Inc. MD5 Message-Digest
  219. Algorithm" in all material mentioning or referencing this software
  220. or this function.
  221.  
  222. License is also granted to make and use derivative works provided
  223. that such works are identified as "derived from the RSA Data
  224. Security, Inc. MD5 Message-Digest Algorithm" in all material
  225. mentioning or referencing the derived work.
  226.  
  227. RSA Data Security, Inc. makes no representations concerning either
  228. the merchantability of this software or the suitability of this
  229. software for any particular purpose. It is provided "as is"
  230. without express or implied warranty of any kind.
  231.  
  232. These notices must be retained in any copies of any part of this
  233. documentation and/or software.
  234.  
  235. =back
  236.  
  237. This copyright does not prohibit distribution of any version of Perl
  238. containing this extension under the terms of the GNU or Artistic
  239. licences.
  240.  
  241. =head1 AUTHOR
  242.  
  243. The MD5 interface was written by Neil Winton
  244. (C<N.Winton@axion.bt.co.uk>).
  245.  
  246. =head1 SEE ALSO
  247.  
  248. perl(1).
  249.  
  250. =cut
  251.